package dashboard.json;

import java.util.List;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.serializer.SerializerFeature;

public class FastJsonUtils {
	 public static String bean2Json(Object obj) {
	       return JSON.toJSONString(obj);
	    }
	     
	    public static String bean2Json(Object obj,String dateFormat) {
	        return JSON.toJSONStringWithDateFormat(obj, dateFormat, SerializerFeature.PrettyFormat);
	   }
	 
	    public static <T> T json2Bean(String jsonStr, Class<T> objClass) {

	        return JSON.parseObject(jsonStr, objClass);
	    }
	    
	    public static JSONObject json2JSONObject(String jsonStr) {
	        return JSON.parseObject(jsonStr);
	    }
	    
	    
	    public static void main(String[] args) {
	        int[] numbers = {1, 1, 2, 3, 5, 8, 13};
	        String[] days = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
	        String numbersJson = bean2Json(numbers);
	        String daysJson = bean2Json(days);
	        System.out.println("numbersJson = " + numbersJson);
	        System.out.println("daysJson = " + daysJson);
	        System.out.println("numbersJson = " + JSON.parseObject(numbersJson, new TypeReference<List<String>>(){}));
	        System.out.println("daysJson = " + JSON.parseObject(daysJson,new TypeReference<List<String>>(){}));
	        
	        String arrayStr = "['北京','上海', '天津', '广州', '深圳', '成都', '重庆', '石家庄', '济南', '青岛', '南京', '杭州', '苏州', '无锡', '合肥', '武汉', '郑州', '长沙', '福州', '厦门', '南宁', '哈尔滨', '长春', '太原', '大连', '沈阳', '东莞']";
	    	JSONArray jsonArray = JSON.parseArray(arrayStr);
		    System.out.println("jsonArray = " + jsonArray);
		   
//		    arrayStr = "北京,上海, 天津, 广州, 深圳";
//		    jsonArray = JSON.parseArray(arrayStr.split(","));
//		    System.out.println("jsonArray = " + jsonArray);
		    
		    String jsonStr = "{'city_name':0,'cost':1,'cost_chain':2,'pv':1,'pv_chain':2,'adpv':1,'adpv_chain':2,'pvr':2,'pvr_chain':2,'disp':1,'disp_chain':2, 'click':1,'click_chain':2,'asn':1,'asn_chain':2,'acp':1,'acp_chain':2,'ctr2':2,'ctr2_chain':2}";
	    	JSONObject jsonObject = JSON.parseObject(jsonStr);
	    	 System.out.println("jsonObject = " + bean2Json(jsonObject,"yyyy-MM-dd"));
	    	 
	    	 jsonStr = "{'queryLocation':[23.9386500000,115.7825750000],'addrList':[{'type':'poi','status':1,'name':'犁滩村','id':'ANB02F102VX0','admCode':'441424','admName':'广东省,梅州市,五华县,','addr':'','nearestPoint':[115.77923,23.93969],'distance':349.817}]}";
	    	 JSONObject jsonObject2 = JSONObject.parseObject(jsonStr, Feature.AllowSingleQuotes);
	    	 System.err.println("jsonObject2 = " + bean2Json(jsonObject2));
	    	 System.err.println("admName = " + jsonObject2.getJSONArray("addrList").getJSONObject(0).getString("admName"));
//	    	 JSONArray array= jsonObject2.getJSONArray("addrList");
//	    	 array.iterator();
		}
	    
}
